﻿using JESAI.HttpRequestMonitor.Core.Dtos;
using JESAI.HttpRequestMonitor.Core.Models;
using JESAI.HttpRequestMonitor.Core.Results;
using JESAI.HttpRequestMonitor.Core.Storage.SqlWhere;
using JESAI.HttpRequestMonitor.Core.Vos;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.HttpRequestMonitor.Core.Abstracts
{
    public interface IRequestMonitorStorage
    {
        /// <summary>
        /// 初始化储存库
        /// </summary>
        /// <returns></returns>
        Task InitAsync();

        /// <summary>
        /// 添加一条请求记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        Task AddRequestInfoAsync(IRequestModel requestModel, IRequestDetailModel requestDetailModel);

        /// <summary>
        /// 获取所有节点信息
        /// </summary>
        /// <returns></returns>
        Task<List<ServerNodeDto>> GetNodesAsync();

        #region 统计

        /// <summary>
        /// 获取Url请求统计
        /// </summary>
        /// <returns></returns>
        Task<List<UrlRequestCountDto>> GetUrlRequestStatisticsAsync(RequestModelSqlWhere sqlWhere);

        /// <summary>
        /// 获取Url的平均响应时间统计
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<List<RequestAvgResponeTimeDto>> GetRequestAvgResponeTimeStatisticsAsync(RequestModelSqlWhere sqlWhere);

        /// <summary>
        /// 获取http状态码数量统计
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<List<StatusCodeCountDto>> GetStatusCodeStatisticsAsync(RequestModelSqlWhere sqlWhere);

        /// <summary>
        /// 获取Url的响应时间分组统计
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<List<ResponeTimeGroupResult>> GetGroupedResponeTimeStatisticsAsync(GroupResponeTimeSqlWhere sqlWhere);

        /// <summary>
        /// 获取首页数据
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<HomePageVo> GetHomePageDataAsync(HomePageDataSqlWhere sqlWhere);

        /// <summary>
        /// 搜索请求信息
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<RequestInfoSearchResult> SearchRequestInfoAsync(RequestModelSearchSqlWhere sqlWhere);

        /// <summary>
        /// 获取请求次数统计
        /// </summary>
        /// <param name="filterOption"></param>
        /// <returns></returns>
        Task<RequestTimesStatisticsResult> GetRequestTimesStatisticsAsync(TimeSpanStatisticsSqlWhere sqlWhere);

        /// <summary>
        /// 获取响应时间统计
        /// </summary>
        /// <param name="filterOption"></param>
        /// <returns></returns>
        Task<ResponseTimeStatisticsResult> GetResponseTimeStatisticsAsync(TimeSpanStatisticsSqlWhere sqlWhere);

        #endregion Statistics

        #region Monitor

        Task<bool> AddMonitorScheduleJob(IMonitorScheduleJobModel job);

        Task<bool> UpdateMonitorScheduleJob(IMonitorScheduleJobModel job);

        Task<bool> DeleteMonitorScheduleJob(string Id);

        Task<IMonitorScheduleJobModel> GetMonitorScheduleJob(string Id);

        Task<List<IMonitorScheduleJobModel>> GetMonitorScheduleJobs();


        #region 查询

        /// <summary>
        /// 获取请求总次数
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<int> GetRequestCountAsync(RequestCountSqlWhere sqlWhere);

        /// <summary>
        /// 依据白名单获取请求次数
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        Task<(int Max, int All)> GetRequestCountWithWhiteListAsync(RequestCountWithListSqlWhere sqlWhere);

        /// <summary>
        /// 获取超时响应统计
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <param name="timeoutThreshold"></param>
        /// <returns></returns>
        Task<int> GetTimeoutResponeCountAsync(RequestCountSqlWhere sqlWhere, int timeoutThreshold);

        #endregion Query

        #endregion Monitor

        Task<RequestMonitorUserModel> CheckLogin(string Username, string Password);

        Task<RequestMonitorUserModel> GetSysUser(string UserName);

        Task<bool> UpdateLoginUser(RequestMonitorUserModel model);

        Task<(IRequestModel, IRequestDetailModel)> GetRequestInfoDetail(string Id);

        Task<IRequestModel> GetRequestInfo(string Id);

        Task<List<IRequestModel>> GetRequestInfoByParentId(string ParentId);

        Task ClearData(string StartTime);

        Task SetLanguage(string Language);

        Task<string> GetSysConfig(string Key);
    }
}
